﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;
using Telecom.Service.EntityCustom;

namespace Telecom.Service
{
    public class thietBiSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();
        private List<int> _listParentIdJson_GianDo { get; set; }
        private LAP_THIETBI LapTbLastFial { get; set; }


        public thietBiSer()
        {
            LapTbLastFial = new LAP_THIETBI() ;
            _listParentIdJson_GianDo = new List<int>();
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public List<DM_THIETBI> getListJsonIntoJtable(string idObject, string type)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                if (type == "all")
                {
                    return dbContext.DM_THIETBI.OrderByDescending(p => p.ID).ToList();
                }
                else if (type == "MVT")
                {
                    decimal[] idLoaiTb = dbContext.LOAI_TBI.Where(p => p.MANGVT == idObject).Select(p => p.MA).ToArray();
                    return dbContext.DM_THIETBI.Where(l => idLoaiTb.Contains(l.LOAITB.Value)).OrderByDescending(p => p.ID).ToList();
                }
                else
                {
                    decimal LoaibTB = decimal.Parse(idObject);
                    return dbContext.DM_THIETBI.Where(l => l.LOAITB == LoaibTB).OrderByDescending(p => p.ID).ToList();
                }
            }
            catch (Exception e)
            {
                List<DM_THIETBI> _listReturn = new List<DM_THIETBI>();
                return _listReturn;
            }
        }

        public List<DM_THIETBI> ListThietBiInJson()
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                return dbContext.DM_THIETBI.ToList();
            }
            catch (Exception e)
            {
                List<DM_THIETBI> _list = new List<DM_THIETBI>();
                return _list;
            }
        }

        public List<DM_THIETBI> ListThietBiInJsonByMVT(string maVt)
        {
            try
            {
                var c = from i in dbContext.DM_THIETBI
                        join k in dbContext.LOAI_TBI on i.LOAITB equals k.MA
                        where k.MANGVT == maVt
                        select i;

                return c.ToList();
            }
            catch (Exception e)
            {
                List<DM_THIETBI> _list = new List<DM_THIETBI>();
                return _list;
            }
        }

        public List<DM_THIETBI> ListThietBiByLoaiTBArray(decimal[] LoaiTBs)
        {
            try
            {
                return dbContext.DM_THIETBI.Where(p => LoaiTBs.Contains(p.LOAITB.Value)).ToList();
            }
            catch (Exception e)
            {
                List<DM_THIETBI> _list = new List<DM_THIETBI>();
                return _list;
            }
        }

        public decimal AddUpDate(DM_THIETBI entity)
        {
            try
            {
                if (entity.ID == 0) dbContext.DM_THIETBI.Add(entity);
                else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return entity.ID;
            }
            catch (Exception e) { return 0; }
        }

        public DM_THIETBI GetByMa(string ma)
        {
            try
            {
                return dbContext.DM_THIETBI.Where(p => p.MA == ma).First();
            }
            catch (Exception e) { return null; }
        }

        public DM_THIETBI GetById(decimal ma)
        {
            try
            {
                return dbContext.DM_THIETBI.Where(p => p.ID == ma).First();
            }
            catch (Exception e) { return null; }
        }

        public DM_THIETBI GetById_Json(decimal ma)
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                return dbContext.DM_THIETBI.Where(p => p.ID == ma).First();
            }
            catch (Exception e) { return null; }
        }

        public bool DeleteTB(decimal id)
        {
            try
            {
                var entity = GetById(id);
                dbContext.DM_THIETBI.Remove(entity);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }



        public List<GianDoEntity> viewGianDoByLapTb(int laptb)
        {
            try
            {
                List<GianDoEntity> _returnJson = new List<GianDoEntity>();

                var LapTbParent = FindLapTBParentNode_GianDo(laptb);
               
                var VNPTSITE = dbContext.VNPT_SITE.Where(p => p.SITEID == LapTbParent.SITEID).First();
                var TBId = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITE.LOAISITE).First();

                
                var VNPTSITE_lastFinnal = dbContext.VNPT_SITE.Where(p => p.SITEID == LapTbLastFial.SITEID).First();
                var TbLastFinnal = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITE_lastFinnal.LOAISITE).First();

                var PortOutParent = dbContext.PORT.Where(p => p.THIETBIID == LapTbParent.ID && p.TB_OUT == LapTbLastFial.ID).First();


                GianDoEntity _ebtityParent = new GianDoEntity();
                _ebtityParent.id = LapTbParent.ID;
                _ebtityParent.name = VNPTSITE.TEN;
                _ebtityParent.Images = TBId.MARKER;
                _ebtityParent.PortSo = ""; // PortOutParent.PORT_SO.ToString();
                if (LapTbParent.ID == laptb) { _ebtityParent.Color = "blue"; }
                else { _ebtityParent.Color = "black"; }

                 GianDoEntity _ebtityParentLastChild = new GianDoEntity();
                 _ebtityParentLastChild.id = LapTbLastFial.ID;
                _ebtityParentLastChild.name = VNPTSITE_lastFinnal.TEN;
                _ebtityParentLastChild.Images = TbLastFinnal.MARKER;
                _ebtityParentLastChild.PortSo = "Port: " + PortOutParent.PORT_SO.ToString();
                if (LapTbLastFial.ID == laptb) { _ebtityParentLastChild.Color = "blue"; }
                else { _ebtityParentLastChild.Color = "black"; }

                AddLapTB_Child_GianDo(_ebtityParentLastChild.children, LapTbLastFial.ID, laptb);

                _ebtityParent.children.Add(_ebtityParentLastChild);

                _returnJson.Add(_ebtityParent);

                
                
                return _returnJson;
            }
            catch (Exception e)
            {
                List<GianDoEntity> _return = new List<GianDoEntity>();
                return _return;
            }
        }

        public void AddLapTB_Child_GianDo(List<GianDoEntity> gianDoJosn, int parentId, int lapTbCurrent)
        {
            var entitylapTb = dbContext.LAP_THIETBI.Where(p => p.ID == parentId).First();
            var entityCauHinhTb = dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == entitylapTb.ID).First();
            var enityListPort = dbContext.PORT.Where(p => p.CAUHINHID == entityCauHinhTb.ID).OrderBy(p => p.PORT_SO).ToList();
            foreach (var item in enityListPort)
            {
                if (item.TB_OUT != null)
                {
                    GianDoEntity _ebtityParent = new GianDoEntity();
                    var LapTbId = dbContext.LAP_THIETBI.Where(p => p.ID == item.TB_OUT).First();
                    
                    var VNPTSITEitem = dbContext.VNPT_SITE.Where(p => p.SITEID == LapTbId.SITEID).First();
                    var TBId = dbContext.LOAI_SITE.Where(p => p.MA == VNPTSITEitem.LOAISITE).First();

                    var PortOutParent = dbContext.PORT.Where(p => p.THIETBIID == parentId && p.TB_OUT == LapTbId.ID).First();

                    _ebtityParent.id = LapTbId.ID;
                    _ebtityParent.name = VNPTSITEitem.TEN;
                    _ebtityParent.Images = TBId.MARKER;
                    _ebtityParent.PortSo = "Port: " + PortOutParent.PORT_SO.ToString();
                    if (LapTbId.ID == lapTbCurrent) { _ebtityParent.Color = "blue"; }
                    else { _ebtityParent.Color = "black"; }

                    AddLapTB_Child_GianDo(_ebtityParent.children, LapTbId.ID, lapTbCurrent);

                    gianDoJosn.Add(_ebtityParent);
                    
                }

            }
        }

        public LAP_THIETBI FindLapTBParentNode_GianDo(int laptbId)
        {
            //LAP_THIETBI _returnJson = new LAP_THIETBI();
            var entityLBTBID = dbContext.LAP_THIETBI.Where(p => p.ID == laptbId).First();
            var entityCauHinhTb = dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == entityLBTBID.ID).First();
            var enityPort = dbContext.PORT.Where(p => p.CAUHINHID == entityCauHinhTb.ID).First();

            if (enityPort.TB_IN != null)
            {
                LapTbLastFial = entityLBTBID;
                return FindLapTBParentNode_GianDo(enityPort.TB_IN.Value);
            }
            else
            {
               return dbContext.LAP_THIETBI.Where(p => p.ID == enityPort.THIETBIID).First();

                //PortLapTbReturn _return = new PortLapTbReturn();
                //_return._LAP_THIETBI = _entityFirst;
                //_return.PortSo = enityPort.PORT_SO.Value;

                //return _return;

            }

        }

        public bool checkDeleteLapTb(int LaptbId)
        {
            try
            {
                bool checkValdtion = false;
                var listCauHinhByLapTb = dbContext.CAUHINH_TBI.Where(p => p.THIETBIID == LaptbId).ToList();
                if (listCauHinhByLapTb.Count > 0)
                {
                    foreach (var item in listCauHinhByLapTb)
                    {
                        var listPort = dbContext.PORT.Where(p => p.CAUHINHID == item.ID &&  p.TRANGTHAI == 2).ToList();
                        if (listPort != null && listPort.Count() > 0)
                        {
                            checkValdtion = true;
                            break;
                        }
                    }
                }
                else
                {
                    checkValdtion = false;
                }

                return checkValdtion;
            }
            catch { return false; }
        }

        public bool checkXoaCauHinhTb(int cauhinhid)
        {
            try
            {
                bool checkValdtion = false;

                var listPort = dbContext.PORT.Where(p => p.CAUHINHID == cauhinhid && p.TRANGTHAI == 2).ToList();
                if (listPort != null && listPort.Count() > 0)
                {
                    checkValdtion = true;
                }
                else
                {
                    checkValdtion = false;
                }

                return checkValdtion;
            }
            catch { return false; }
        }

    }

    public class PortLapTbReturn
    {
        public LAP_THIETBI _LAP_THIETBI { get; set; }
        public decimal PortSo { get; set; }
        public PortLapTbReturn()
        {
            _LAP_THIETBI = new LAP_THIETBI();
        }
    }

}
